import axios from "axios";
import { solver_url, resource_url, computing_url, elements_url } from "./url";

const headers = {
  Authorization:
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjcyMzExOTMsImlhdCI6MTcyNzE0NDc5MywiaXNzIjoic2Yud29yayIsInN1YiI6IntcImZpcnN0TGFuZGluZ1wiOjEsXCJndWlkXCI6XCI1MmMxZGZkZTM5MWY0ZjYzYjVmODRlMDY1NGMwNjEzN1wiLFwiaWRcIjoyLFwicGhvbmVcIjpcIjE1NTAwMDAwMDAwXCIsXCJ1c2VyQ29kZVwiOlwiYWRtaW5cIixcInVzZXJOYW1lXCI6XCLotoXnuqfnrqHnkIblkZhcIn0ifQ.29LREjzawIx_d6F6vfm1fOutfZvr8ZsByGSpZyombk8", // 示例：添加认证 token
};

export const createTask = async (data: any): Promise<any> => {
  return axios
    .post(solver_url + "/dc-solver-scheduler/api/v1/task/create", data)
    .then((res) => res.data);
};

export const getBucketList = async (): Promise<any> => {
  return axios
    .get(resource_url + "/user/resource/tree/bucket/list", {
      withCredentials: true,
      headers: headers,
    })
    .then((res) => res.data);
};

// 根据桶名获取初始路径列表
export const getPathList = async (params: any): Promise<any> => {
  return axios
    .get(
      resource_url + `/user/resource/tree/path/list?bucket=${params.bucket}`,
      {
        withCredentials: true,
        headers: headers,
      }
    )
    .then((res) => res.data);
};

// 获取当前目录下所有文件以及子目录
export const getObjectList = async (params: any): Promise<any> => {
  return axios
    .post(resource_url + `/user/resource/read/object/list`, params, {
      withCredentials: true,
      headers: headers,
    })
    .then((res) => res.data);
};

// 分页获得求解器
export const getSolverList = async (): Promise<any> => {
  return axios
    .post(computing_url + `/solver/page`, {
      pageNum: 1,
      pageSize: 9999,
    })
    .then((res) => res.data);
};

// 上传文件
export const uploadFile = async (params: any): Promise<any> => {
  return axios
    .post(resource_url + `/user/resource/write/object/upload`, params, {
      withCredentials: true,
      headers: {
        ...headers,
        "Content-Type": "multipart/form-data",
        Accept: "*/*",
      },
    })
    .then((res) => res.data);
};

/**
 * * 获取任务详情
 * @param id - 任务id
 */
export const getTaskDetail = async (id: number): Promise<any> => {
  return axios.get(computing_url + `/taskSolver/${id}`).then((res) => res.data);
};

// 要素库模型列表api集成
export const getModelList = async (): Promise<any> => {
  return axios
    .get(elements_url, {
      params: {
        pageNum: 1,
        pageSize: 999,
        isAsc: "descending",
        orderByColumn: "createTime",
      },
    })
    .then((res) => res.data);
};
